package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private BlackListApi blackListApi;

    //查询通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();

        //1、获取用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //2、获取用户的手机号码
        String phone = UserHolder.getPhone();
        vo.setPhone(phone);
        //3、获取用户的陌生人问题
        Question question = questionApi.findByuserId(userId);
        String txt = question == null ? "你喜欢java吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //4、获取用户的APP通知开关数据
        Settings settings = settingsApi.findByuserId(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;

    }

    /**
     * 设置陌生人问题
     *
     * @param content
     */
    public void saveQuestion(String content) {
        // 1、需要根据用户的id查询陌生人问题
        Long userId = UserHolder.getUserId();
        //2、存在陌生人问题，执行修改操作
        Question question = questionApi.findByuserId(userId);
        //3、不存在陌生人问题，直接进行新增
        if (question == null) {
            //3.1 如果不存在 保存

            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //3.2 存在就更新
            question.setTxt(content);
            questionApi.update(question);

        }

    }

    /**
     * 设置通知 保存
     *
     * @param map
     */

    public void saveOrUpdateSettings(Map map) {
        // 0、从Map中获取请求参数
        boolean likeNotification = (boolean) map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");

        // 1、获取当前登录的用户id
        Long userId = UserHolder.getUserId();

        // 2、根据id查询通知设置
        Settings settings = settingsApi.findByuserId(userId);

        // 3、如果不存在该用户的通知设置，执行保存操作
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);

        } else {  // 4、如果存在通知设置执行更新操作
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.updae(settings);
        }
    }

    /**
     * 黑名单翻页列表
     *
     * @param page
     * @param size
     * @return
     */
    //分页查询黑名单列表
    public PageResult blacklist(int page, int size) {

        //1、获取当前用户的id
        Long userId = UserHolder.getUserId();
        //2、调用API查询用户的黑名单分页列表  Ipage对象
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, size);
        //3、对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pr = new PageResult(page, size, iPage.getTotal(), iPage.getRecords());
        //4、返回
        return pr;
    }

    /**
     * 移除黑名单
     *
     * @param blackUserId
     */
    //取消黑名单
    public void deleteBlackList(Long blackUserId) {
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();
        //2、调用api删除
        blackListApi.delete(userId, blackUserId);
    }
}



